<h2>Why is this an issue?</h2>
<p>It is inefficient to build a <code>String</code> from a <code>StringBuilder</code> or <code>StringBuffer</code> just to check if it’s empty.
Instead, directly use the <code>.isEmpty</code> method.</p>
<h3>Noncompliant code example</h3>
<pre data-diff-id="1" data-diff-type="noncompliant">
StringBuilder sb = new StringBuilder();
// ...
if ("".equals(sb.toString()) { // Noncompliant
  // ...
}
if (sb.toString().isEmpty()) { // Noncompliant
  // ...
}
</pre>
<h3>Compliant solution</h3>
<pre data-diff-id="1" data-diff-type="compliant">
StringBuilder sb = new StringBuilder();
// ...
if (sb.isEmpty()) {
  // ...
}
if (sb.isEmpty()) {
  // ...
}
</pre>
<h3>Benchmarks</h3>
<table>
  <colgroup>
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
    <col style="width: 20%;">
  </colgroup>
  <thead>
    <tr>
      <th>Method</th>
      <th>stringBuilderSize</th>
      <th>Runtime</th>
      <th>Average time</th>
      <th>Error margin</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><p>isEmpty</p></td>
      <td><p>10</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.57 ns/op</p></td>
      <td><p>±0.38 ns/op</p></td>
    </tr>
    <tr>
      <td><p>isEmpty</p></td>
      <td><p>100</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.68 ns/op</p></td>
      <td><p>±0.10 ns/op</p></td>
    </tr>
    <tr>
      <td><p>isEmpty</p></td>
      <td><p>1000</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.80 ns/op</p></td>
      <td><p>±0.12 ns/op</p></td>
    </tr>
    <tr>
      <td><p>length</p></td>
      <td><p>10</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.83 ns/op</p></td>
      <td><p>±0.12 ns/op</p></td>
    </tr>
    <tr>
      <td><p>length</p></td>
      <td><p>100</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.66 ns/op</p></td>
      <td><p>±0.11 ns/op</p></td>
    </tr>
    <tr>
      <td><p>length</p></td>
      <td><p>1000</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>6.67 ns/op</p></td>
      <td><p>±0.07 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringEquals</p></td>
      <td><p>10</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>13.92 ns/op</p></td>
      <td><p>±0.18 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringEquals</p></td>
      <td><p>100</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>59.09 ns/op</p></td>
      <td><p>±0.53 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringEquals</p></td>
      <td><p>1000</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>465.79 ns/op</p></td>
      <td><p>±5.86 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringIsEmpty</p></td>
      <td><p>10</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>13.83 ns/op</p></td>
      <td><p>±0.23 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringIsEmpty</p></td>
      <td><p>100</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>60.06 ns/op</p></td>
      <td><p>±3.42 ns/op</p></td>
    </tr>
    <tr>
      <td><p>toStringIsEmpty</p></td>
      <td><p>1000</p></td>
      <td><p>Temurin 21</p></td>
      <td><p>484.58 ns/op</p></td>
      <td><p>±4.24 ns/op</p></td>
    </tr>
  </tbody>
</table>
<p><strong>Benchmarking code</strong></p>
<p>The results were generated by running the following snippet with <a href="https://github.com/openjdk/jmh">JMH</a>.</p>
<pre>
@Param({"10", "100", "1000"})
int stringBuilderSize;


private StringBuilder sb;

@Setup(Level.Iteration)
public void setup() {
  sb = new StringBuilder();
  IntStream.range(0, stringBuilderSize).forEach(i -&gt; sb.append("word"));
}

@Benchmark
public StringBuilder toStringEquals() {
  if ("".equals((sb.toString()))) {
    return sb;
  }
  return new StringBuilder();
}

@Benchmark
public StringBuilder toStringIsEmpty() {
  if (sb.toString().isEmpty()) {
    return sb;
  }
  return new StringBuilder();
}

@Benchmark
public StringBuilder length() {
  if (sb.length() == 0) {
    return sb;
  }
  return new StringBuilder();
}

@Benchmark
public StringBuilder isEmpty() {
  if (sb.isEmpty()) {
    return sb;
  }
  return new StringBuilder();
}
</pre>

